package com.flute.icrawler.framework.framework;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.flute.icrawler.app.processor.AbstractProcessor;

public class ProcessorClassList implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4246421665360163545L;

	private List<String> processorKeys;

	public ProcessorClassList() {
		initProcessorList();
	}

	public void initProcessorList() {
		processorKeys = new ArrayList<String>();
	}

	public synchronized void addProcessor(AbstractProcessor abstractProcessor) {
		this.processorKeys.add(abstractProcessor.ID);
	}

	public synchronized void addProcessor(String processorKey) {
		this.processorKeys.add(processorKey);
	}

	public synchronized void remove(AbstractProcessor abstractProcessor) {
		this.processorKeys.remove(abstractProcessor.ID);
	}

	public synchronized List<String> getProcessorKeys() {

		return processorKeys;
	}

	public boolean contains(AbstractProcessor processorClass) {
		return processorKeys.contains(processorClass.ID);
	}

	public boolean contains(String processorKey) {
		return processorKeys.contains(processorKey);
	}

	public int size() {
		return processorKeys.size();
	}

	public boolean containsAll(ProcessorClassList list) {

		return processorKeys.containsAll(list.getProcessorKeys());
	}

	public void addAll(ProcessorClassList list) {
		processorKeys.addAll(list.getProcessorKeys());
	}

	/**
	 * 排除掉传入ProcessorClassList的剩余ProcessorClassList
	 */
	public ProcessorClassList getExcludeClassList(ProcessorClassList list) {
		ProcessorClassList remain = new ProcessorClassList();
		for (String processorKey : processorKeys) {
			if (!list.contains(processorKey)) {
				remain.addProcessor(processorKey);
			}
		}

		return remain;
	}

	/**
	 * 取出当前和传入ProcessorClassList的交集
	 */
	public ProcessorClassList getIncludeClassList(ProcessorClassList list) {
		ProcessorClassList remain = new ProcessorClassList();
		for (String processorKey : processorKeys) {
			if (list.contains(processorKey)) {
				remain.addProcessor(processorKey);
			}
		}

		return remain;
	}

	public String toKey() {
		StringBuffer sb = new StringBuffer();
		for (String p : processorKeys) {
			sb.append(p);
		}

		return sb.toString();
	}
}
